package com.huawei.codelab.slice;

import com.huawei.codelab.ResourceTable;
import com.huawei.codelab.utils.ApiUtil;
import com.huawei.codelab.utils.HttpRequestUtil;
import com.huawei.codelab.utils.LoadImageUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;

public class DetailAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_people_detail);
        String userName1 = (String)intent.getParams().getParam("userName");
        Integer userId = (Integer)intent.getParams().getParam("userId");
        String userImgAddress1 = (String)intent.getParams().getParam("userImgAddress");
        String userCall1 = (String)intent.getParams().getParam("userCall");
        Integer userAge1 = (Integer)intent.getParams().getParam("userAge");

        TextField calltext = (TextField)findComponentById(ResourceTable.Id_user_call1);
        calltext.setHint(userCall1);
        TextField nametext = (TextField)findComponentById(ResourceTable.Id_user_name1);
        nametext.setHint(userName1);
        Picker picker1 = (Picker)findComponentById(ResourceTable.Id_user_age2);
        Picker picker2 = (Picker)findComponentById(ResourceTable.Id_user_age);
        //年龄字符串分割
        String strage = userAge1.toString();
        //十岁以下
        if(userAge1<10){
            char c = strage.charAt(0);
            int a =c-'0';
            picker1.setValue(0);
            picker2.setValue(a);
        }
        //十岁以上
        else{
            char a = strage.charAt(0);
            char b = strage.charAt(1);
            int aa =a-'0';
            int bb =b-'0';
            picker1.setValue(aa);
            picker2.setValue(bb);
        }

        getUITaskDispatcher().asyncDispatch(()->{
            Image image = (Image)findComponentById(ResourceTable.Id_user_image);
            String imgUrl = ApiUtil.IMG_BASE_PATH+userImgAddress1;
            LoadImageUtil.loadImg(this,imgUrl,image);
        });
        Button update = (Button)findComponentById(ResourceTable.Id_btn_update);
        //提交修改响应
        update.setClickedListener(component -> {
            TaskDispatcher globalTaskDispatcher =this.getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            globalTaskDispatcher.asyncDispatch(()->{

                String userCall = calltext.getText();
                if(userCall.equals("")){
                    userCall=calltext.getHint();
                }
                String userName = nametext.getText();
                if(userName.equals("")){
                    System.out.println("----------------------------------进去则表示userName为空（非null）");
                    userName=nametext.getHint();
                }
                //年龄处理
                int userAge =picker1.getValue()*10+picker2.getValue();
                String urlString = "http://120.26.194.73:9001/user/update?userId="+userId+"&userCall="+userCall+"&userName="+userName+"&userAge="+userAge;
                System.out.println("-------------------------请求地址"+urlString);
                String result = HttpRequestUtil.sendGetRequest(this,urlString);
                if(result.equals("成功")){
                    System.out.println("---------------------------------更新成功");
                }else{
                    System.out.println("-----------------------------------更新失败");
                }
            });
            //设置跳转回到主页滑动第二页----->成员管理中
//            MainAbilitySlice_max master = new MainAbilitySlice_max();
            //无法设置跳转，跳转时，界面为初始化好，暂未解决，放弃
//            master.tabList.selectTabAt(1);
            present(new MainAbilitySlice_max(),new Intent());
        });

        Button delete = (Button)findComponentById(ResourceTable.Id_btn_delete);
        delete.setClickedListener(component -> {
            TaskDispatcher globalTaskDispatcher =this.getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            globalTaskDispatcher.asyncDispatch(()->{
                String urlString = "http://120.26.194.73:9001/user/delete?userId="+userId;
                String result = HttpRequestUtil.sendGetRequest(this, urlString);
                if(result.equals("成功")){
                    System.out.println("---------------------------------更新成功");
                }else{
                    System.out.println("-----------------------------------更新失败");
                }
                this.present(new MainAbilitySlice_max(),new Intent());
            });
        });


    }
}
